package com.eduask.action;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.eduask.beans.Client;
import com.eduask.beans.Manager;
import com.eduask.service.ClientService;
import com.eduask.service.ManagerService;


@Scope(value = "prototype")
@Controller
public class ClientAction {

	@Autowired
	private ClientService clientService;
	
	@Autowired
	private ManagerService managerService;
	
	@RequestMapping("dengLu")
	public String login(@ModelAttribute Client client, String identity, HttpSession session, Map<String, Object> map) {
		System.out.println("client: "+client);
		System.out.println("identity: "+identity);
		System.out.println("session: "+session);
		if("manager".equals(identity)) {
			List<Manager> managers = managerService.login(client.getName(), client.getPassword());
			if(managers.size() != 0) {
				session.setAttribute("identity", identity);
				session.setAttribute("name", client.getName());
				return "forward:index.jsp";
			}
		}
		if("normal".equals(identity)) {
			List<Client> clients = clientService.login(client.getName(), client.getPassword());
			if(clients.size() != 0) {
				session.setAttribute("identity", identity);
				session.setAttribute("name", client.getName());
				session.setAttribute("id", (Integer)clients.get(0).getId());
				return "forward:index.jsp";
			}
		}
		map.put("client", new Client());
		map.put("message", "用户名或密码错误，请重新输入！！！");
		return "forward:index.jsp";
	}
	
	@RequestMapping("/loginOut")
	public String loginOut(HttpSession session) {
		session.invalidate();
		return "forward:index.jsp";
	}
	
	@RequestMapping("/saveClient")
	public String saveClient(@ModelAttribute Client client) {
		System.out.println(client);
		clientService.save(client);
		return "zhucheSuccess";
	}
}
